package listing

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"os"
)

func HandFileList(writer http.ResponseWriter,
	request *http.Request) error {
	// 拿到list 后面具体的文件名，或者目录
	fileName := request.URL.Path[len("/list/"):]  // /list/fib.txt, 通过切片，只要后面的。fib.txt
	fmt.Println(fileName)
	// 打开文件
	fibPath := fmt.Sprintf("%s%s", "../defer/", fileName)
	file, err := os.Open(fibPath)
	if err != nil {
		return err
	}
	defer file.Close()
	// 读取文件
	all, err := ioutil.ReadAll(file)
	if err != nil {
		return err
	}
	// 内容写入到写入到 ResponseWriter 里去
	writer.Write(all)

	return nil
}
